package com.jshcbd.modules.asset.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshcbd.modules.asset.entity.AssetEntity;
import com.jshcbd.modules.asset.vo.AssetVo;
import com.jshcbd.modules.asset.vo.ChartVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * @author zhoucd
 * @date
 * @description
 */
public interface AssetMapper extends BaseMapper<AssetEntity> {


    @Update("update asset set state = 2 ,receive_id = #{receiveId},location_building = #{building},location_floor = #{floor}," +
            "start_usetime = coalesce(start_usetime,current_timestamp)  where id = #{assetId} ")
    void updateAsset2Receive(String assetId,String receiveId,Integer building,Integer floor);

    @Update("update asset set state = 1 ,receive_id = null   where id = #{assetId} ")
    void updateAsset2Return(String assetId);

    @Update("update asset set state = 1 ,receive_id = null,use_state = 2 where id = #{assetId} ")
    void updateAsset2Damage(String assetId);


    @Update("update asset set state = 3,dis_date=#{disDate} where id = #{assetId} ")
    void updateDis(String assetId, Date disDate);



    @Select("SELECT * FROM (SELECT A.*,B.CODE,B.CLASSES,B.TYPE_NAME,B.DEPRECIATION_MONTH," +
            "C.RECEIVER,C.TELPHONE,C.COMPANY_ID,C.DUE_DATE,C.SUM_PRICE,C.NOTE RECEIVE_NOTE,D.NAME COMPANY_NAME " +
            "FROM ASSET A " +
            "LEFT JOIN CLASSES B ON A.CLASSES_ID = B.ID " +
            "LEFT JOIN RECEIVE C ON A.RECEIVE_ID = C.ID " +
            "LEFT JOIN COMPANY D ON C.COMPANY_ID = D.ID) " +
            " TEMP  ${ew.customSqlSegment}")
    Page<AssetVo> pageView(Page<AssetVo> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT * FROM (SELECT A.*,B.CODE,B.CLASSES,B.TYPE_NAME,B.DEPRECIATION_MONTH," +
            "C.RECEIVER,C.TELPHONE,C.COMPANY_ID,C.DUE_DATE,C.SUM_PRICE,C.NOTE RECEIVE_NOTE,D.NAME COMPANY_NAME " +
            "FROM ASSET A " +
            "LEFT JOIN CLASSES B ON A.CLASSES_ID = B.ID " +
            "LEFT JOIN RECEIVE C ON A.RECEIVE_ID = C.ID " +
            "LEFT JOIN COMPANY D ON C.COMPANY_ID = D.ID) TEMP  ${ew.customSqlSegment}")
    List<AssetVo> listView(@Param(Constants.WRAPPER) Wrapper wrapper);


    @Select("SELECT STATE GROUP_BY,COUNT(1) FROM ASSET GROUP BY STATE")
    List<ChartVo> queryPieByState();

    @Select("SELECT CLASSES GROUP_BY,COUNT(1) FROM (SELECT B.CLASSES  FROM ASSET A LEFT JOIN CLASSES B ON A.CLASSES_ID = B.ID) TEMP GROUP BY CLASSES")
    List<ChartVo> queryPieByClasses();

    @Select("SELECT TYPE_NAME GROUP_BY,COUNT(1) FROM (SELECT B.TYPE_NAME  FROM ASSET A LEFT JOIN CLASSES B ON A.CLASSES_ID = B.ID) TEMP GROUP BY TYPE_NAME")
    List<ChartVo> queryPieByTypeName();


    @Select("SELECT  EXTRACT(MONTH FROM IN_DATE) GROUP_BY ,COUNT(1) FROM ASSET " +
            "WHERE  EXTRACT(YEAR FROM IN_DATE) = #{year} GROUP BY  EXTRACT(MONTH FROM IN_DATE) ORDER BY EXTRACT(MONTH FROM IN_DATE)")
    List<ChartVo> queryByInstoreDate(Integer year);







}
